package com.regis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.regis.entity.db.Category;
import com.regis.entity.db.Dish;
import com.regis.entity.db.Setmeal;
import com.regis.entity.db.SetmealDish;
import com.regis.entity.dto.SetmealDto;
import com.regis.service.CategoryService;
import com.regis.service.SetmealDishService;
import com.regis.service.SetmealService;
import com.regis.mapper.SetmealMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 纱雾休比酱
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2022-07-28 03:58:08
*/
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 同步保存套餐和套餐_菜品表
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息
        this.save(setmealDto);

        //保存套餐和菜品的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void delete(Long... ids) {
        //删除套餐与菜品的连接
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId,
                Arrays.asList(ids)));
        //删除套餐
        this.remove(new LambdaQueryWrapper<Setmeal>().in(Setmeal::getId,Arrays.asList(ids)));
    }

    /**
     * 根据id查询一条套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto get(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);
        //获取套餐菜品信息
        List<SetmealDish> setmealDishes =
                setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        setmealDto.setSetmealDishes(setmealDishes);
        //获取套餐分类的名称
        Category category = categoryService.getById(setmealDto.getCategoryId());
        setmealDto.setCategoryName(category.getName());

        return setmealDto;
    }

    /**
     * 同时更新套餐和套餐_菜品表
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //更新套餐的基本信息
        this.updateById(setmealDto);

        //更新套餐和菜品的关联信息
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,
                setmealDto.getId()));
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}




